package com.process.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.process.controller.util.Code;
import com.process.controller.util.R;
import com.process.dao.outer.AdminDataAnalysisDao;
import com.process.dao.outer.CampusOffPublishDao;
import com.process.dao.outer.CampusOffPublishVoDao;
import com.process.dao.outer.OutDataAnalysisDao;
import com.process.dao.team.ProcessTaskVoDao;
import com.process.dao.voDao.CampusOuterSubtaskVoDao;
import com.process.dao.voDao.OutTeacherVoDao;
import com.process.dao.voDao.PracticalTaskVoDao;
import com.process.domain.outer.*;
import com.process.domain.publi.PracticalTask;
import com.process.domain.publi.PracticalTaskVo;
import com.process.domain.publish.CampusInnerPublish;
import com.process.domain.publish.CampusInnerPublishVo;
import com.process.domain.publish.TaskBaseVo;
import com.process.domain.team.ProcessTaskVo;
import com.process.service.out.CampusOffSubtaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @ClassName: OuterTeacher
 * @Author: 殷凡卓
 * @Date: 2023/4/26 17:41
 */
@RestController
@Slf4j
@RequestMapping("/outer")
public class OuterTeacherController {

    @Autowired
    private OutTeacherVoDao outTeacherVoDao;
    @Autowired
    private PracticalTaskVoDao practicalTaskVoDao;
    @Autowired
    private CampusOffPublishDao campusOffPublishDao;
    @Autowired
    private CampusOffSubtaskService campusOffSubtaskService;
    @Autowired
    private CampusOffPublishVoDao campusOffPublishVoDao;
    @Autowired
    private ProcessTaskVoDao processTaskVoDao;
    @Autowired
    private CampusOuterSubtaskVoDao campusOuterSubtaskVoDao;
    @Autowired
    private AdminDataAnalysisDao adminDataAnalysisDao;
    @Autowired
    private OutDataAnalysisDao outDataAnalysisDao;


    @GetMapping("/myDetail")
    public R getMyDetailByTeacherPhone(ServletRequest servletRequest) {
        String phone = (String) servletRequest.getAttribute("username");
        OutTeacherVo outTeacherVo = outTeacherVoDao.selectMyDetilByPhone(phone);
        return new R(Code.GET_OK, outTeacherVo, "获取我的详细信息");
    }

    @GetMapping("/practicalTaskVos")
    public R getMypracticalTaskVos(ServletRequest servletRequest) {
        String phone = (String) servletRequest.getAttribute("username");
        List<PracticalTaskVo> practicalTaskVos = practicalTaskVoDao.selectVoByOuterAdviserPhone(phone);
        return new R(Code.GET_OK, practicalTaskVos, "获取我的practicalTaskVos");
    }

    @PostMapping("outerPublish")
    public R setInnerPublish(@RequestBody CampusOffPublishAo campusOffPublishAo, ServletRequest servletRequest) {
        CampusOffPublish campusOffPublish = campusOffPublishAo.getCampusOffPublish();
        List<CampusOffSubtask> campusOffSubtasks = campusOffPublishAo.getCampusOffSubtasks();
        System.out.println(campusOffPublish);
        System.out.println(campusOffSubtasks);

        int insert = campusOffPublishDao.insert(campusOffPublish);

        long publishId = campusOffPublish.getPublishId();
        //      遍历——给子任务表设置外键
        for (CampusOffSubtask offSubtask : campusOffSubtasks) {
            offSubtask.setPublishId(publishId);
        }
        boolean saveBatch = campusOffSubtaskService.saveBatch(campusOffSubtasks);
        return new R(Code.SAVE_OK, saveBatch, "outet_publish 返回的是是否成功");
    }

    @GetMapping("outerClass")
    public R getOuterClass(ServletRequest servletRequest) {

        String username = (String) servletRequest.getAttribute("username");
        Integer identity = (Integer) servletRequest.getAttribute("identity");
        List<PracticalTask> practicalTasks = practicalTaskVoDao.selectClassByOuterAdviserPhone(username);
        return new R(Code.GET_OK, practicalTasks, "查询inner_Class");
    }

    @GetMapping("/myouterPublish")
    public R getmyouterPublish(ServletRequest servletRequest) {
        String phone = (String) servletRequest.getAttribute("username");
        List<CampusInnerPublishVo> campusInnerPublishVos = campusOffPublishVoDao.selectByOutTeacherPhone(phone);
        return new R(Code.GET_OK, campusInnerPublishVos, "老师查看自己的发布表");
    }

    @GetMapping("/myouterProcessTask")
    public R getmyouterProcessTask(ServletRequest servletRequest) {
        //获取我的电话号码
        String phone = (String) servletRequest.getAttribute("username");
        List<ProcessTaskVo> processTaskVos = processTaskVoDao.selectMyOuterProcessTaskByTeacherPhone(phone);
        return new R(Code.SAVE_OK, processTaskVos, "查看我的inner过程任务");
    }

    @GetMapping("outTaskbaseVo/{practicalId}")
    public R getTaskbaseVo(@PathVariable long practicalId, ServletRequest servletRequest) {
        List<CampusOuterSubtaskVo> campusOuterSubtaskVos = campusOuterSubtaskVoDao.selectoutByPublishId(practicalId);

        return new R(Code.GET_OK, campusOuterSubtaskVos, "select_course_taskbase");
    }

    @GetMapping("deleteouterPublish/{publishId}")
    public R deleteouterPublishByPublishId(@PathVariable long publishId, ServletRequest servletRequest) {
//        获取我的电话号码
//        String username = (String) servletRequest.getAttribute("username");

        int delete1 = campusOffPublishDao.delete(new LambdaQueryWrapper<CampusOffPublish>().eq(CampusOffPublish::getPublishId, publishId));
//        int delete = campusInnerPublishDao.delete(new LambdaQueryWrapper<CampusInnerPublish>().eq(CampusInnerPublish::getPublishId, publishId));
        if (delete1 > 0)
            return new R(Code.DELETE_OK, delete1, "删除outerpublish成功");
        else
            return new R(Code.DELETE_ERR, delete1, "删除outerpublish失败");

    }

    @GetMapping("/OutdataAnalysis")
    public R OutdataAnalysis(ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");
        List<Map<String, Object>> maps1 = outDataAnalysisDao.dataFindThree(username);
       // List<Map<String, Object>> maps = adminDataAnalysisDao.dataFindThree();
      //  List<Map<String,Object>> data1 = adminDataAnalysisDao.dataFindOne();

        return new R(Code.GET_OK, maps1, "获取最高分最低分平均分成功！");

    }

    @GetMapping("/InnerdataAnalysis")
    public R InnerdataAnalysis(ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");

        List<Map<String, Object>> maps = outDataAnalysisDao.dataFindFive(username);
       // List<Map<String, Object>> maps1 = outDataAnalysisDao.dataFindThree(username);
        // List<Map<String, Object>> maps = adminDataAnalysisDao.dataFindThree();
        //  List<Map<String,Object>> data1 = adminDataAnalysisDao.dataFindOne();

        return new R(Code.GET_OK, maps, "获取最高分最低分平均分成功！");

    }


    @GetMapping("/OutdataAnalysisTwo")
    public R OutdataAnalysisTwo(ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");

        List<Map<String, Object>> maps = outDataAnalysisDao.dataFindFoure(username);

        return new R(Code.GET_OK, maps, "获取最高分最低分平均分成功！");

    }

}
